package com.yds.modewithprinceple.strategy;

import java.util.HashMap;
import java.util.Map;

/**
 * @author YDS
 * @date 2021/5/10
 * @discribe 策略工厂
 */
@SuppressWarnings("all")
public class StrategyFactory {

    private static final Map<String, IPayListener> strategies = new HashMap<>();   //支付策略容器
    public static final String PAY_TYPE_A = "alipay";      //默认支付类型
    public static final String PAY_TYPE_W = "wechatpay";   //默认支付类型
    private static final String DEFAULT_TYPE = PAY_TYPE_W;  //默认支付类型

    static {
        strategies.put(PAY_TYPE_A, new AliPayService());
        strategies.put(PAY_TYPE_W, new WeChatPayService());
    }

    /**
     * 默认支付策略
     *
     * @return
     */
    public static IPayListener getStrategy() {
        return getStrategy(DEFAULT_TYPE);
    }

    /**
     * 指定支付策略
     *
     * @param type
     * @return
     */
    public static IPayListener getStrategy(String type) {
        IPayListener iPayListener = strategies.get(type);
        if (iPayListener == null) {
            throw new IllegalArgumentException("pay type is not exist");
        }
        return iPayListener;
    }

}
